﻿using Ibp.Service.Entity.ODAEntity;
using OdaOperation.Core.Creators;
using OdaOperation.Core.Parameters;
using OdaOperation.DB;
using OdaOperation.Model;
using Teigha.TD;

namespace OdaOperation.Extends
{
    public static class ExtendBlockReference
    {
        // 扩展方法：在块表记录中插入块
        public static void ExeInsertBlockTableRecord(this DbDatabase db, OdBlockModel odBlock)
        {
            // 调用 DbDatabase 的 InsertBlockTableRecord 方法插入块
            db.InsertBlockTableRecord(odBlock);
        }

        // 扩展方法：添加块引用
        public static void AddBlockReference(this DbDatabase db, ReferenceParameter parameter)
        {
            // 使用参数创建新的块引用
            new BlockReferenceCreator(db.Database, parameter.BlockName, parameter.LayerName).Create(parameter);
        }

        // 重载方法：添加块引用（使用块名、层名和点）
        public static void AddBlockReference(this DbDatabase db, string blockName, string layerName, DPoint point)
        {
            // 使用块名、层名和点创建新的块引用
            new BlockReferenceCreator(db.Database, blockName, layerName).Create(point);
        }

        // 扩展方法：设置块引用的参数
        public static void SetParameter(this OdDbBlockReference reference, ParameterSet parameter)
        {
            // 创建动态块引用
            OdDbDynBlockReference odDbDynBlockReference = new OdDbDynBlockReference(reference.objectId());
            // 创建动态块引用属性数组
            OdDbDynBlockReferencePropertyArray odDbDynBlockReferencePropertyArray = new OdDbDynBlockReferencePropertyArray();
            // 获取块属性
            odDbDynBlockReference.getBlockProperties(odDbDynBlockReferencePropertyArray);

            // 遍历属性数组
            foreach (OdDbDynBlockReferenceProperty item in odDbDynBlockReferencePropertyArray)
            {
                // 过滤隐藏和只读属性
                if (!item.show() || item.readOnly())
                {
                    continue;
                }

                // 获取属性值
                object value = parameter.GetValue(item.propertyName());
                if (value == null)
                {
                    continue;
                }

                // 根据属性类型设置属性值
                OdResBuf.ValueType valueType = item.propertyType();
                OdResBufArray odResBufArray = new OdResBufArray();
                OdResBuf odResBuf = OdResBuf.newRb((int)valueType);

                // 根据属性类型转换值
                switch (valueType)
                {
                    case OdResBuf.ValueType.kDxfReal:
                        if (double.TryParse(value.ToString(), out var result2))
                        {
                            odResBuf.setDouble(result2);
                        }
                        break;
                    case OdResBuf.ValueType.kDxfText:
                        odResBuf.setString(value.ToString());
                        break;
                    case OdResBuf.ValueType.kDxfInt16:
                        if (short.TryParse(value.ToString(), out var result4))
                        {
                            odResBuf.setInt16(result4);
                        }
                        break;
                    case OdResBuf.ValueType.kDxfInt32:
                        if (int.TryParse(value.ToString(), out var result3))
                        {
                            odResBuf.setInt32(result3);
                        }
                        break;
                    case OdResBuf.ValueType.kRtDouble:
                        if (double.TryParse(value.ToString(), out var result))
                        {
                            odResBuf.setDouble(result);
                        }
                        break;
                }

                // 设置属性值
                item.setValue(odResBuf);
            }
        }
    }

}